/*

	File-Name: 2-variables.do
	Last Updated: 10/29/2020
	Author: Janica Magat
	Last Updated: Janica Magat
	Purpose: Generate variables for analysis
	
*/

* Generate treatment indicator

tab treat_assg
encode treat_assg, gen(treatment)
label define treatment 1 "Control" 2 "Formalization Only" 3 "PG/LIRS" 4 "Enforcement/LIRS" 5 "PG/MPA" 6 "Enforcement/MPA", modify
label values treatment treatment

// by messenger
generate T_messenger = 2 if treat_assg == "T2" | treat_assg == "T3"
replace T_messenger = 1 if treat_assg == "T4" | treat_assg == "T5"
replace T_messenger = 0 if treat_assg == "T1"
label var T_messenger "Tax Appeal by Messenger"
label define T_messenger 0 "No Tax Appeal" 1 "Informal Agent" 2 "State Agent"
label values T_messenger T_messenger

// T2 and T3 were visited by LIRS  
generate T_lirs = 1 if treat_assg == "T2" | treat_assg == "T3"
replace T_lirs = 0 if treat_assg == "T1" | treat_assg == "T4" | treat_assg == "T5"
label var T_lirs "Messenger is LIRS"
label define T_lirs 0 "Not Tax Agent" 1 "Tax Agent"
label values T_lirs T_lirs

// T4 and T5 were visited by market association 
generate T_marketassoc = 1 if treat_assg == "T4" | treat_assg == "T5"
replace T_marketassoc = 0 if treat_assg == "T1" | treat_assg == "T2" | treat_assg == "T3"
label var T_marketassoc "Messenger is Market Association"
label define T_marketassoc 0 "Not Market Agent" 1 "Market Agent"
label values T_marketassoc T_marketassoc

// by message
generate T_message = 2 if treat_assg == "T2" | treat_assg == "T4"
replace T_message = 1 if treat_assg == "T3" | treat_assg == "T5"
replace T_message = 0 if treat_assg == "T1"
label var T_message "Tax Appeal by Message"
label define T_message 0 "No Tax Appeal" 1 "Enforcement" 2 "Public Goods"
label values T_message T_message

* Generate Assignment Probabilities
generate T_int2_prob = 1/3 if treat_assg == "T1"
replace T_int2_prob = 2/3 if treat_assg == "T2" | treat_assg == "T3" | treat_assg == "T4" | treat_assg == "T5"
label var T_int2_prob "The respondent’s probability of being in the assigned condition T"

* Generate Inverse Probabilities
generate invT_int2_prob = 1/T_int2_prob 

* Generate outcome and control variables
// actual formalization
recode q74_yellow_card (99 = 0), gen(etcc_e) // following Metaketa codebook
label var etcc_e "Do you have an e-TCC?"
label define etcc_e 0 "No" 1 "Yes", modify
label values etcc_e etcc_e

* strictest definition: enumerator has to see the card
gen etcc1 = etcc_e
replace etcc1 = 0 if q74_1_obs_yellow_card == 0 

* middling definition: answered yes to providing the date of issuance, even if the card wasn't on them
gen etcc2 = etcc1
replace etcc2 = 1 if q84_1_date_ask == 2 | q84_1_date_ask == 1
label var etcc2 "Registered for e-TCC"

// New measures of tax compliance: 
* this mirrors Metaketa treatment of tax payment as not credible if respondent says not liable for tax.
gen compliance3 = q45_lag_paid_e
replace compliance3 = 0 if q45_lag_paid_e == 99
replace compliance3 = 0 if q45_lag_req_tax_e == 99 
replace compliance3 = 0 if q45_lag_req_tax_e == 0
label var compliance3 "Compliance if liable"
label values compliance3 compliance_e

* new measure of compliance conditional on etcc: 
gen paidetcc=compliance3
replace paidetcc=0 if etcc2==0
replace paidetcc=0 if q45_year_pay_income<2018

// tax morale
gen tax_morale2_e = 1 if q40_citizen_pay_tax == 4
replace tax_morale2_e = 2 if q40_citizen_pay_tax == 3
replace tax_morale2_e = 3 if q40_citizen_pay_tax == 2
replace tax_morale2_e = 4 if q40_citizen_pay_tax == 1

label var tax_morale2_e "Tax Morale"

// expectation of costs
* likelihood of punishment, LIRS
generate punish_lirs_e = 1 if q50_lirs_punish == 4
replace punish_lirs_e = 2 if q50_lirs_punish == 3
replace punish_lirs_e = 3 if q50_lirs_punish == 2
replace punish_lirs_e = 4 if q50_lirs_punish == 1
replace punish_lirs_e = . if q50_lirs_punish == 99
label var punish_lirs_e "How likely is it that LIRS would punish people who do not pay income tax?"
label define punish_lirs_e 0 "Don't Know/Refused" 1 "Very unlikely" 2 "Unlikely" 3 "Likely" 4 "Very Likely" , modify
label values punish_lirs_e punish_lirs_e

* likelihood of punishment, market association
generate punish_market_e = 1 if q52_mrkt_punish == 4
replace punish_market_e = 2 if q52_mrkt_punish == 3
replace punish_market_e = 3 if q52_mrkt_punish == 2
replace punish_market_e = 4 if q52_mrkt_punish == 1
replace punish_market_e = . if q52_mrkt_punish == 99
label var punish_market_e "How likely is it that the marketplace association would punish people who do not pay income tax?"
label define punish_market_e 0 "Don't Know/Refused" 1 "Very unlikely" 2 "Unlikely" 3 "Likely" 4 "Very Likely"
label values punish_market_e punish_market_e

* likelihood of punishment, Lagos State
recode q55_nos_punished_e (99 = .), gen(punish_state_e)

* expected punishment
center punish_state punish_market punish_lirs , pre(z_) st
egen punish_new = rowmean(z_*)
label var punish_new "Expectation of Punishment, Index" 

// expectation of benefits
* loan application
gen loan_apply_e = q21b_applied_biz_loan
replace loan_apply_e = 0 if loan_apply_e == 2
label var loan_apply_e "Have you applied for one of those loans?"
label define loan_apply_e 0 "No" 1 "Yes"
label values loan_apply_e loan_apply_e

* secondary school enrollment
recode q20b_chldrn_enrolled (99 = .) (1 = 0) (2 3 = 1), gen(kid_enroll_e)
label define kid_enroll_e 0 "Yes, enrolled" 1 "No, not enrolled"
label values kid_enroll_e kid_enroll_e

* expected benefit
center loan_apply_e kid_enroll_e, pre(z_) st
egen benefit_new=rowmean(z_*)
label var benefit_new "Expectation of Benefit, Index" 

// recode to two levels of relative trust for ease of interpretation 
gen trust2 = 0 if trust == 2
replace trust2 = 1 if trust == 0 | trust == 1
label define trust2 0 "Trust MPA more" 1 "Trust LIRS weakly more"
label values trust2 trust2	

// binary yoruba concentration
gen byoruba = 0 if pyoruba<=.5
replace byoruba = 1 if pyoruba>.5 

// marketplace quality index
* component variables
gen mpacrime = 0 // Who do you think would be most helpful in solving this crime?
replace mpacrime = 1 if q21_b_b == 2

gen mpaactive = 0 // Do you regularly participate in any clubs or associations by attending meetings or raising money?
replace mpaactive = 1 if q26_1_b == 1
replace mpaactive = 1 if q27_b < 3 

gen mpaproblem = 0 // What do you think is the most important problem facing vendors here in this marketplace?
replace mpaproblem = 1 if q37_b == 3 
* reverse mpaproblem
replace mpaproblem = 1-mpaproblem

gen mpaequal = 0 // Would you say it represents the interests of all vendors equally?
replace mpaequal = 1 if q38_b == 1

gen mpasolve = 0 // Who would you approach to solve the problem?
replace mpasolve = 1 if q39_b == 1

gen mpafee = 0 // Is there a tax or some other fee that all vendors in this marketplace cannot avoid paying? 
replace mpafee = 1 if q42_b == 1
replace mpafee = 1 if q44_b == 1

label var mpaactive "Participates in MPA"
label var mpasolve "Would Approach MPA to Solve Problem"
label var mpafee "MPA Collects Tax/Fee They Must Pay"

* mpa quality index
egen mpaquality = rowmean(mpacrime mpaactive mpaproblem mpaequal mpasolve mpafee)
label var mpaquality "MPA Quality"

egen pmpaquality = median(mpaquality)
gen mpaquality_d = 0 if mpaquality <= pmpaquality
replace mpaquality_d = 1 if mpaquality > pmpaquality
label var mpaquality_d "MPA Quality"
label define mpaquality_d 0 "Below-median MPA quality" 1 "Above-median MPA quality", modify
label values mpaquality_d mpaquality_d 

drop pmpaquality
bysort mname: egen pmpaquality=mean(mpaquality)
label var pmpaquality "Average MPA Quality"

// access to services (baseline)
* component variables
foreach x in a b c d e f g h {
	gen service_`x' = q12_`x'_b
	replace service_`x' = . if q12_`x'_b == 99
}

* service access index
egen service_access_b = rowmean(service*)
label var service_access_b "Access to Services Index"

xtile service_access_d_b=service_access_b
replace service_access_d_b=service_access_d_b-1
label define service_access_d_b 0 "Below-median access to services" 1 "Above-median access to services", modify
label values service_access_d_b service_access_d_b 
label var service_access_d_b "Access to Services"
	
// access to services by government providers (baseline)
* component variables
foreach x in a b c d e f g h {
	gen govservice_`x' = 0
	replace govservice_`x' = 1 if q12_`x'_b == 1 & (q14_`x'_b == 1 | q14_`x'_b == 2)
}

* gov service access index
egen govservice_access_b = rowmean(govservice*)
label var govservice_access_b "Access to Government Services"

egen pgovservice_access_b = median(govservice_access_b)
gen govservice_access_d_b = 0 if govservice_access_b <= pgovservice_access_b
replace govservice_access_d_b = 1 if govservice_access_b > pgovservice_access_b
label define govservice_access_d_b 0 "Below-median access to government services" 1 "Above-median access to government services", modify
label values govservice_access_d_b govservice_access_d_b 
label var govservice_access_d_b "Access to Government Services"

// alternative club membership
generate club2=club
replace club2=1 if club==2
label var club2 "Club Membership"
label define club2 0 "Not a Member" 1 "Club Member"
label values club2 club2

// size of shop
gen shopsize = f6_b
replace shopsize = 3 if f6_b == 4
label define shopsize 1 "1 Danfo bus" 2 "2 Danfo buses" 3 "3 Danfo buses", modify
label values shopsize shopsize
label var shopsize "Size of shop"

gen shopsize2 = shopsize
replace shopsize2 = 2 if shopsize == 3
label define shopsize2 1 "1 Danfo bus" 2 "2-3 Danfo buses", modify
label values shopsize2 shopsize2
label var shopsize2 "Size of shop"

// generate dummies for analysis using the interflex command
* market size : reference category = 1
generate market_size_2 = 1 if market_size == 2
replace market_size_2 = 0 if market_size_2 == . 

generate market_size_3 = 1 if market_size == 3
replace market_size_3 = 0 if market_size_3 == .

generate market_size_4 = 1 if market_size == 4
replace market_size_4 = 0 if market_size_4 == .  

* rent_dummy : reference category = 0
gen rent_dummy_1 = 1 if rent_dummy == 1
replace rent_dummy_1 = 0 if rent_dummy_1 == .

gen rent_dummy_2 = 1 if rent_dummy == 2
replace rent_dummy_2 = 0 if rent_dummy_2 == .

gen rent_dummy_3 = 1 if rent_dummy == 3
replace rent_dummy_3 = 0 if rent_dummy_3 == .

* trust : reference category = 0
gen trust_1 = 1 if trust == 1
replace trust_1 = 0 if trust_1 == .

gen trust_2 = 1 if trust == 2
replace trust_2 = 0 if trust_2 == .

gen trust_3 = 1 if trust == 3
replace trust_3 = 0 if trust_3 == .

* club : reference category = 0
gen club_1 = 1 if club == 1
replace club_1 = 0 if club_1 == .

gen club_2 = 1 if club == 2
replace club_2 = 0 if club_2 == .

* interactions 
generate lirs_mpaquality = T_lirs*mpaquality
generate marketassoc_govservice = T_marketassoc*govservice_access_b
generate marketassoc_pyoruba = T_marketassoc*pyoruba

// generate enumerator co-ethnicity variable
gen enumerator_yoruba_e = 99 if f17_ethnicity_e == .
replace enumerator_yoruba_e = 0 if f17_ethnicity_e != 23 & f17_ethnicity_e != .
replace enumerator_yoruba_e = 1 if f17_ethnicity_e == 23 & f17_ethnicity_e != .
label define enumerator_yoruba_e 0 "Not Yoruba" 1 "Yoruba" 99 "Unknown", modify
label values enumerator_yoruba_e enumerator_yoruba_e

gen enumerator_coethnic = 1 if enumerator_yoruba_e == yoruba
replace enumerator_coethnic = 0 if enumerator_yoruba_e != yoruba
replace enumerator_coethnic = 99 if enumerator_yoruba_e == 99
label var enumerator_coethnic "Enumerator Co-ethnicity"
label define enumerator_coethnic 0 "Not Co-ethnic" 1 "Co-ethnic" 99 "Unknown", modify
label values enumerator_coethnic enumerator_coethnic

// generate variables for balance tables (baseline)
* ethnicity
gen yoruba_b = 1 if q75_b == 3
replace yoruba_b = 0 if yoruba_b == .

gen igbo_b = 1 if q75_b == 2
replace igbo_b = 0 if igbo_b == .

gen hausa_b = 1 if q75_b == 1
replace hausa_b = 0 if hausa_b == .

* language
gen english_b = 1 if q3_b == 1
replace english_b = 0 if english_b == .

gen yorubalang_b = 1 if q3_b == 2
replace yorubalang_b = 0 if yorubalang_b == .

gen igbolang_b = 1 if q3_b == 3
replace igbolang_b = 0 if igbolang_b == .

gen hausalang_b = 1 if q3_b == 4
replace hausalang_b = 0 if hausalang_b == .

gen pidgin_b = 1 if q3_b == 5
replace pidgin_b = 0 if pidgin_b == .

* invest with same ethnicity
gen invest_b = 1 if q57_b == 1 | q57_b == 2
replace invest_b = 0 if q57_b == 3 | q57_b == 4

* ethnic tensions in the marketplace
gen tension = 1 if q35_b == 1 | q35_b == 2
replace tension = 0 if q35_b == 3 | q35_b == 88
label define tension 1 "yes, there is tension" 0 "no, there is no tension"
label values tension tension 

* born in lagos - q84_b

* income 
recode q19_1reord_b (99 = .), gen(income)

* imports
recode q8_b (99 = .) (2 = 0), gen(imports)

* education
recode q83_b (99 = .) (9 = 0), gen(educ)

* age
recode q70_b (99 = .), gen(age)

* trust in mpa
recode trust (3 = .) (1 = 0) (2 = 1), generate(trustmarketmore)

* participation
gen mpa_partic = 1 if q26_1_b == 1 | q27_b < 3 
replace mpa_partic = 0 if mpa_partic == .
replace mpa_partic = . if q26_1_b == 99 | q27_b == 99

* unavoidable mpa fee
gen mpa_fee = 0
replace mpa_fee = 1 if q42_b == 1

* trust in lirs
recode q49_e_b (99 = .), gen(trust_lirs_b)
label var trust_lirs_b "Trust in LIRS"
label define trust_lirs_b 1 "No trust at all" 2 "Some trust" 3 "Trust" 4 "Great deal of trust" 0 "No response recorded/Refused", modify
label values trust_lirs_b trust_lirs_b

* trust in market association
recode q49_b_b (99 = .), gen(trust_market_b)
label var trust_market_b "Trust in MPA"
label define trust_market_b 1 "No trust at all" 2 "Some trust" 3 "Trust" 4 "Great deal of trust" 0 "No response recorded/Refused", modify
label values trust_market_b trust_market_b

* trust in local gov 
recode q49_c_b (99 = .), gen(trust_lga_b)

* trust in state - fed govt
recode q49_f_b (99 = .), gen(trust_fed_b)

* trust in state - police
recode q21_a_b (99 = .) (2 = 0), gen(trust_police_b)

* business loan
recode q48_b (99 = .) (2 = 0), gen(biz_loan)

* heard of lirs
recode q45_b (99 = .) (2 = 0), gen(heard_lirs)

* lirs visit
recode q46_b (99 = .) (2 = 0), gen(visit_lirs)
replace visit_lirs = 1 if q47_b == 1

* paid a bribe
recode q59_b (99 = .) (2 = 0), gen(bribe)

* market rent - rent

* tax morale (baseline)
gen tax_morale_b = 1 if q40_reord_b == 4
replace tax_morale_b = 2 if q40_reord_b == 3
replace tax_morale_b = 3 if q40_reord_b == 2
replace tax_morale_b = 4 if q40_reord_b == 1

* employees
recode q6_b (99 = .), gen(employees)

* incumbent support
generate apc = 0 if q34_b != .
replace apc = 1 if q34_b == 1 

label var yoruba_b "Ethnicity: Yoruba"
label var igbo_b "Ethnicity: Igbo"
label var hausa_b "Ethnicity: Hausa"
label var english_b "Language: English"
label var yorubalang_b "Language: Yoruba"
label var igbolang_b "Language: Igbo"
label var hausalang_b "Language: Hausa"
label var pidgin_b "Language: Pidgin"
label var invest_b "Co-ethnic investment"
label var tension "Ethnic tension"
label var q84_b "Lagos-born"
label var income "Income"
label var imports "Imports"
label var educ "Education"
label var age "Age"
label var mpa_partic "MPA participation"
label var mpa_fee "MPA fee"
label var trustmarketmore "Trust MPA more than LIRS"
label var trust_market_b "Trust in MPA"
label var trust_lirs_b "Trust in LIRS"
label var trust_lga_b "Trust in local government"
label var trust_fed_b "Trust in federal government"
label var trust_police_b "Trust in police"
label var biz_loan "Business loan awareness"
label var heard_lirs "Heard of LIRS"
label var visit_lirs "Contacted by LIRS"
label var bribe "Paid bribes"
label var rent "Rent"
label var tax_morale_b "Tax morale"
label var market_size "Market size"
label var friends_market "Friends in market"
label var yoruba "Yoruba"
label var lirs_connect "LIRS connection"
label var club2 "Club membership"
label var employees "Employees"
label var apc "Incumbent supporter"
label var govservice_access_b "Government service access"

// alternative spec of trust (as suggested by Pia's replication class)
* relative trust as a continuous variable
gen relative_trust = trust_market_b - trust_lirs_b + 3 // higher values indicate more trust in MPA
label var relative_trust "Relative trust in MPA"

egen prelative_trust = median(relative_trust)
gen relative_trust_d = 0 if relative_trust <= prelative_trust
replace relative_trust_d = 1 if relative_trust > prelative_trust
label var relative_trust_d "Relative trust in MPA (Binary)"
label define relative_trust_d 0 "Below-median MPA trust" 1 "Above-median MPA trust", modify
label values relative_trust_d relative_trust_d

* generate market minority indicator
gen marketminority = 0
replace marketminority = 1 if yoruba == 1 & pyoruba < .5
replace marketminority = 1 if yoruba == 0 & pyoruba > .5

* MPA information
recode pq4_mpainfo_p (2 = 0), gen(mpainfo)
label var mpainfo "Received information about election from MPA"

bysort mname_b: egen pmpainfo = mean(mpainfo)
xtile bmpainfo = pmpainfo

* bulwark markets
gen tradefair = 0
replace tradefair = 1 if mname_b == "aspanda market" | mname_b == "bba market"

gen igbomarket = 0
replace igbomarket = 1 if tradefair == 1 | mname_b == "computer village" | mname_b == "alaba rago market"

gen bulwark=0
replace bulwark = 1 if tradefair == 1 | mname == "capa ram market" | mname == "coker buildings  materials" | mname == "trinity market zone c"

* social capital
gen marketfriends = 0
replace marketfriends = 1 if q62_o1_b != . & q62_o1_b!=7
replace marketfriends = 2 if q62_o2_b != .
replace marketfriends = 3 if q62_o3_b != .
replace marketfriends = 4 if q62_o4_b != .
replace marketfriends = 5 if q62_o5_b != .
replace marketfriends = 6 if q62_o6_b != .
label var marketfriends "Number of friends in market"

* ethnicity of market friends
generate samemarketfriends=0 if marketfriends!=0
foreach i of numlist 1/6{
replace samemarketfriends=1 if q62_o1_b==`i' & q68_`i'_b==1
}

foreach i of numlist 2/6{
replace samemarketfriends= samemarketfriends + (q62_o2_b==`i' & q68_`i'_b==1)
}

foreach i of numlist 3/6{
replace samemarketfriends= samemarketfriends + (q62_o3_b==`i' & q68_`i'_b==1)
}

foreach i of numlist 4/6{
replace samemarketfriends= samemarketfriends + (q62_o4_b==`i' & q68_`i'_b==1)
}

foreach i of numlist 5/6{
replace samemarketfriends= samemarketfriends + (q62_o5_b==`i' & q68_`i'_b==1)
}

foreach i of numlist 6/6{
replace samemarketfriends= samemarketfriends + (q62_o6_b==`i' & q68_`i'_b==1)
}

generate sharemarketsame=samemarketfriends/marketfriends if marketfriends!=0

gen samefriends=0
foreach v of var q68_1_b-q68_6_b{
replace samefriends = samefriends + (`v'==1)
}

gen diffriends=0
foreach v of var q68_1_b-q68_6_b{
replace diffriends = diffriends + (`v'==2)
}

gen sharesame=samefriends/(samefriends+diffriends) // careful, there are 505 with 0's in the denominator, so this should be interpreted as for among anyone with any friends

gen friends=0 if q61_b=="0"
replace friends=0 if q61_b=="00"
replace friends=0 if q61_b=="000"
replace friends=0 if q61_b=="99"
replace friends=1 if sharesame<1
replace friends=2 if sharesame==1

bysort mname: egen psharemarketsame = mean(sharemarketsame)
egen uniquemarket = tag(mname)
